package com.mouse.magent.proxy.service;

import com.mouse.magent.common.net.TcpServer;
import com.mouse.magent.common.util.PropUtil;
import com.mouse.magent.proxy.handler.HttpServiceHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HttpClient {
    public void start() throws Exception {
        Integer port = Integer.valueOf(PropUtil.getValue("http.agent.port", "8585"));
        TcpServer natxClientServer = new TcpServer();
        natxClientServer.bind(port, new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().
                        addLast("decoder", new HttpRequestDecoder()).
                        addLast("httpObject", new HttpObjectAggregator(65536)).
                        addLast("encoder", new ByteArrayEncoder()).
                        addLast("httpservice", new HttpServiceHandler());
            }
        });
        log.info("load http proxy success,port:{}", port);
    }

    public static void main(String[] args) throws Exception {
        new HttpClient().start();
    }
}
